package com.learning.demos.jvmgc;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author adolphw
 * <p>
 * -XX:MetaSpaceSize=8m -XX:MaxMetaSpaceSize=8m
 * <p>
 *
 * Caused by: java.lang.OutOfMemoryError: Metaspace
 * 虚拟机加载类的信息
 * 常量池
 * 静态变量
 * 即时编译后的代码
 */
public class OOMMetaSpaceDemo {
    static class OOMTest {

    }

    public static void main(String[] args) {

        int i = 0;
        try {
            while (true) {
                i++;
                Enhancer enhancer = new Enhancer();
                enhancer.setSuperclass(OOMTest.class);
                enhancer.setUseCache(false);
                enhancer.setCallback(new MethodInterceptor() {
                    @Override
                    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                        return methodProxy.invokeSuper(o, args);
                    }
                });
                enhancer.create();
            }
        } catch (Exception e) {
            System.out.println("多少次之后发生异常：" + i);
            e.printStackTrace();
        }
    }
}
